package com.yonyougov.devops.cruxcisupport.thread;

import com.yonyougov.devops.cruxcisupport.server.ServerEntity;
import com.yonyougov.devops.cruxcisupport.server.ServerService;
import com.yonyougov.devops.cruxcisupport.server.dto.ExecuteCmdCommandDto;
import com.yonyougov.devops.cruxcisupport.server.vo.ExecuteCmdCommandVo;
import com.yonyougov.devops.cruxcisupport.util.SshUtil;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


@Slf4j
@Component
public class CheckServerThreadPoolExecutor {


    @Autowired
    private ServerService serverService;

    private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

    //@PostConstruct
    //public void start(){
    //    log.info("开始执行服务检查线程");
    //    SshUtil sshUtil = new SshUtil();
    //    executorService.schedule(()->{
    //        log.info("开始执行服务检查线程");
    //        ServerEntity build = ServerEntity.builder().build();
    //        build.setPageAble(false);
    //        Pagination<ServerEntity> all = serverService.findAllBy(build);
    //        for (ServerEntity data : all.getDatas()) {
    //            try {
    //                boolean check = sshUtil.check(data, 2);
    //                if(check){
    //                    System.out.println("data.getId() 在线");
    //                    serverService.update(ServerEntity.builder().id(data.getId()).state(1).build());
    //                }else{
    //                    System.out.println("data.getId() 下线");
    //                    serverService.update(ServerEntity.builder().id(data.getId()).state(0).build());
    //                }
    //            } catch (Exception e) {
    //                System.out.println("捕获到异常");
    //            }
    //        }
    //    //},60, TimeUnit.MINUTES);
    //    },60, TimeUnit.SECONDS);
    //}
}
